package com.godme.sort;

import java.util.Arrays;

public class QuickSort {


    public static void main(String[] args) {
        int[] arr = {9,8,7,6,5,4,3,2,1};
        sort(arr);
        System.err.println(Arrays.toString(arr));
    }


    public static void sort(int[] src){
        sort(src, 0, src.length-1);
    }



    public static void change(int arr[], int a, int b){
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void sort(int[] src, int left, int right){
        if(left >=right){
            return;
        }
        int leftCursor = left;
        int rightCursor = right;
        int temp = src[left];

        while(leftCursor < rightCursor){
            while(leftCursor < rightCursor && src[leftCursor] < temp){
                leftCursor ++;
            }
            while(leftCursor < rightCursor && src[rightCursor] >= temp){
                rightCursor --;
            }

            change(src, leftCursor, rightCursor);
        }
        src[leftCursor] = temp;
        sort(src, left, rightCursor);
        sort(src, rightCursor+1, right);
    }


    /**
     * 优化
     *  1. 中值（有序高效）
     *  2. 连续重复值(重复值区间免对比)
     *  3. 小区间替换算法(插入排序)
     */
}
